package com.song.su.controller;

import com.song.su.mapper.BookDetailMapper;
import com.song.su.model.BookDetail;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.List;

@Controller
public class SearchController {
    @Autowired
    BookDetailMapper bookDetailMapper;

    @GetMapping("/search")
    public String search(){
        return "search";
    }

    @PostMapping("/search")
    public String postSearch(@RequestParam(name = "search") String search,
                             Model model){

        try {
            List<BookDetail> bookDetails = bookDetailMapper.findByname(search);
            model.addAttribute("bookDestails",bookDetails);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "search";
    }
}
